﻿using STMS.BLL;
using STMS.WSTMSystem.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using STMS.Models.DModels;
using Common;

namespace STMS.WSTMSystem.Product
{
    public partial class FrmProductInfo : Form
    {
        public FrmProductInfo()
        {
            InitializeComponent();
        }
        public event Action ReloadProductList;//刷新产品列表
        private FInfoData fModel = null;
        int productId = 0;//当前的产品编号
        ProductBLL proBLL = new ProductBLL();
        string oldName = "";

        private void FrmProductInfo1_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                fModel = this.Tag as FInfoData;
                InitProductInfo();
            }
        }

        private void InitProductInfo()
        {
            if (fModel != null)
            {
                string pageTypeName = "新增";
                switch (fModel.ActType)
                {
                    case 1:
                        ClearInfo();
                        btnClear.Enabled = true;
                        pageTypeName = "新增";
                        break;
                    case 2:
                        productId = fModel.FId;
                        LoadProduct(productId);
                        btnClear.Enabled = false;
                        pageTypeName = "修改";
                        break;
                    default: break;
                }
                this.Text += "----" + pageTypeName;
            }
        }

        private void LoadProduct(int productId)
        {
            ProductInfo product = proBLL.GetProduct(productId);
            if (product != null)
            {
                txtProNo.Text = product.ProductNo;
                txtProName.Text = product.ProductName;
                txtLowTemper.Text = product.FitLowTemper.ToString();
                txtHighTemper.Text = product.FitHighTemper.ToString();
                oldName = product.ProductName;
            }
        }

        private void ClearInfo()
        {
            txtProNo.Clear();
            txtProName.Clear();
            txtLowTemper.Clear();
            txtHighTemper.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string proName = txtProName.Text.Trim();
            string proNo = txtProNo.Text.Trim();
            decimal? lowTemper = null;
            decimal? highTemper = null;
            if (!string.IsNullOrEmpty(txtLowTemper.Text))
                lowTemper = txtLowTemper.Text.GetDecimal();
            if (!string.IsNullOrEmpty(txtHighTemper.Text))
                highTemper = txtHighTemper.Text.GetDecimal();
            if (CheckProductInfo(proName, lowTemper, highTemper) == false) return;
            ProductInfo proInfo = new ProductInfo()
            {
                ProductId = productId,
                ProductName = proName,
                ProductNo = proNo,
                FitLowTemper = lowTemper,
                FitHighTemper = highTemper
            };
            bool bl = false;
            if (fModel.ActType == 1)
            {
                int reNewId = proBLL.AddProductWithId(proInfo);
                if (reNewId > 0)
                {
                    bl = true;
                    productId = reNewId;
                    oldName = proName;
                }
            }
            else
            {
                bl = proBLL.UpdateProduct(proInfo);
            }
            string msgTitle = "产品信息";
            string typeName = fModel.ActType == 1 ? "新增" : "修改";
            if (bl)
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"产品:{proName} {typeName} 成功！");
                if (fModel.ActType == 1)
                    fModel.ActType = 2;
                ReloadProductList?.Invoke();
            }
            else
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"产品:{proName} {typeName} 失败！");
                return;
            }
        }

        private bool CheckProductInfo(string proName, decimal? lowTemper, decimal? highTemper)
        {
            string msgTitle = "产品信息";
            if (string.IsNullOrEmpty(proName))
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "产品名称不能为空！");
                return false;
            }
            if (oldName != "" && proName != oldName || oldName == "")
            {
                if (proBLL.ExistsProduct(ProductName))
                {
                    MsgBoxHelper.MsgErrorShow(msgTitle, "产品名称已存在！");
                    return false;
                }
            }
            if (lowTemper != null && highTemper != null && highTemper < lowTemper)
            {
                MsgBoxHelper.MsgErrorShow("产品信息", "产品的适合低温必须小于高温！");
                return false;
            }
            return true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearInfo();
            fModel.ActType = 1;
            productId = 0;
            oldName = "";
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
